/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycity.jsonops;

import com.mycity.dao.CommentTab;
import com.mycity.dao.PostTab;
import com.mycity.util.StringEncode;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

/**
 *
 * @author celikeins
 */
public class PostOps {

    public static PostTab[] getPostsByUserId(Long userId) {
        String url = "services/user/showAllPosts?userId=" + userId;

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = StringEncode.toTagArray(tags);
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Long createPost(PostTab newOne) {
        String url = "services/user/post?userId=" + newOne.getUserId()
                + "&postType=" + newOne.getPostType() + "&postContent=" + StringEncode.encode(newOne.getPostContent() + "|" + newOne.getPicId())
                + "&tags=" + StringEncode.encode(StringEncode.toTag(newOne.getTags())) + "&locationId=" + newOne.getLocId() + "&locationName=" + StringEncode.encode(newOne.getLocName());
        System.out.println("url:" + url);
        try {
            return JSONParse.readLongFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean createComment(CommentTab newOne) {
        String url = "services/user/commentOnPost?userId=" + newOne.getOwnerId()
                + "&commentContent=" + StringEncode.encode(newOne.getContent())
                + "&postId=" + newOne.getPostId();
        System.out.println("url:" + url);
        try {
            return JSONParse.readLongFromUrl(url) >= 0;
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static PostTab getPostById(Long postId) {
        String url = "services/user/getPostByPostId?postId=" + postId;

        PostTab post = null;
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                String tags = (String) result.get("tags");
                String[] tagArray = StringEncode.toTagArray(tags);
                post = new PostTab((Long) result.get("userId"), (Long) result.get("postId"),
                        (Long) result.get("postType"), (String) result.get("postDate"), (String) result.get("postContent"), tagArray, (Long) result.get("locationId"),
                        (String) result.get("locationName"));
                return post;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static PostTab[] getRecentPosts() {
        String url = "services/user/showRecentPosts";

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = StringEncode.toTagArray(tags);
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static CommentTab[] getCommentsOfPost(Long postId) {
        String url = "services/user/getCommentsOfPost?postId=" + postId;
        CommentTab[] comments = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                comments = new CommentTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }

                    comments[i] = new CommentTab((Long) temp.get("userId"), (Long) temp.get("postId"), (String) temp.get("commentContent"), (Long) temp.get("hasStar"), (Long) temp.get("commentId"), (String) temp.get("commentDate"));
                }
                return comments;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;

    }

    public static PostTab[] searchPostsByTag(String search) {
        String url = "services/search/searchPostByTag?tag=" + StringEncode.encode(search);

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = null;
                    if (tags != null) {

                        tagArray = StringEncode.toTagArray(tags);
                    }
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static PostTab[] searchPostsByLoc(Long search) {
        String url = "services/search/searchPostByLocation?locationId=" + search;

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = null;
                    if (tags != null) {
                        tagArray = StringEncode.toTagArray(tags);
                    }
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static PostTab[] searchPostsNearby(Long search) {
        String url = "services/search/nearbyPosts?locationId=" + search;

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = null;
                    if (tags != null) {
                        tagArray = StringEncode.toTagArray(tags);
                    }
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static PostTab[] recommend(Long userId) {
        String url = "services/user/recommend?userId=" + userId;

        PostTab[] posts = null;
        try {
            JSONArray result = (JSONArray) JSONParse.readJSONFromUrl(url);
            if (result != null) {
                JSONObject temp;
                int size = result.size();
                posts = new PostTab[size];
                for (int i = 0; i < size; i++) {
                    temp = (JSONObject) result.get(i);
                    if (temp == null) {
                        return null;
                    }
                    String tags = (String) temp.get("tags");
                    String[] tagArray = null;
                    if (tags != null) {
                        tagArray = StringEncode.toTagArray(tags);
                    }
                    posts[i] = new PostTab((Long) temp.get("userId"), (Long) temp.get("postId"),
                            (Long) temp.get("postType"), (String) temp.get("postDate"), (String) temp.get("postContent"), tagArray, (Long) temp.get("locationId"),
                            (String) temp.get("locationName"));
                }
                return posts;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static String convertToLocation(Long id) {
        String url = "https://api.instagram.com/v1/locations/" + id + "?client_id=bbe7c585542f41dd924f1c96e63bc591";
        String loc = "Middle of the Nowhere";
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromFullUrl(url);
            if (result != null) {
                JSONObject data = (JSONObject) result.get("data");
                loc = (String) data.get("name");
                return loc;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return loc;
        }
        return loc;
    }
    
    public static Double[] convertToCoordinates(Long id) {
        String url = "https://api.instagram.com/v1/locations/" + id + "?client_id=bbe7c585542f41dd924f1c96e63bc591";
        Double[] loc = new Double[2];
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromFullUrl(url);
            if (result != null) {
                JSONObject data = (JSONObject) result.get("data");
                loc[0] = (Double) data.get("latitude");
                loc[1] = (Double) data.get("longitude");
                return loc;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return loc;
        }
        return loc;
    }

    public static Boolean giveStar(Long commentId) {
        String url = "services/user/giveStar?commentId=" + commentId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readLongFromUrl(url) > 0;
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean giveDiamond(Long postId, Long userId) {
        String url = "services/user/giveDiamond?postId=" + postId + "&userId=" + userId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readLongFromUrl(url) > 0;
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;
    }

    public static Boolean isPostDiamonded(Long postId, Long userId) {
        String url = "services/user/isPostDiamonded?userId=" + userId + "&postId=" + postId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;

    }

    public static Boolean deleteComment(Long commentId) {
        String url = "services/user/deleteComment?commentId=" + commentId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;

    }

    public static Boolean deletePost(Long postId) {
        String url = "services/user/deletePost?postId=" + postId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;
    }

    public static Boolean removeStar(Long commentId) {
        String url = "services/user/removeStar?commentId=" + commentId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;
    }

    public static Boolean removeDiamond(Long postId, Long userId) {
        String url = "services/user/removeDiamond?postId=" + postId + "&userId=" + userId;
        System.out.println("url:" + url);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return false;
        }
        return false;
    }
}
